﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data;
using Data.implement;
using log4net;
using LYWebsite.Convert;
using LYWebsite.Helper;
using LYWebsite.Models;
using Services.Implement;
using Services.Interface;

namespace LYWebsite.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Login/

        //public ActionResult Index()
        //{
        //    return View();
        //}
        ILog Logger = LogManager.GetLogger("");

        public ActionResult Index()
        {
            try
            {
                Session.Clear();
                string customerAddress = Request.UserHostAddress;
                IInitPageService initPageService = new InitPageService();
                initPageService.AddIndexRecord(customerAddress);

                IList<SelectListItem> result = new List<SelectListItem>();
                result.Add(new SelectListItem() { Text = "1", Value = "1" });
                result.Add(new SelectListItem() { Text = "2", Value = "2" });
                ViewData["carList"]=result;

                return View("Index");
            }
            catch (Exception e)
            {
                Logger.Error("Index has an error:"+e);
                return Redirect("~/Result/Exception");
            }
            
        }

        private bool GetSessionWeiXinUser()
        {
            var httpCookie = Request.Cookies["userInfo"];
            if (httpCookie != null)
            {
                WeiXinUser userInfo = new WeiXinUser();
                userInfo.city = httpCookie["city"];
                userInfo.country = httpCookie["country"];
                userInfo.headimgurl = httpCookie["headimgurl"];
                userInfo.nickname = httpCookie["nickname"];
                userInfo.openId = httpCookie["openId"];
                userInfo.province = httpCookie["province"];
                userInfo.sex = httpCookie["sex"];
                Session["UserInfo"] = userInfo;
                return true;
            }
            return false;
        }

        [HttpPost]
        public ActionResult Register(int CarId)
        {
            try
            {
                if (GetSessionWeiXinUser())
                    return Redirect("~/Charging/InitUser/?CarId=" + CarId); ;
                string appid = ConfigurationSettings.AppSettings["appid"];
                string url = ConfigurationSettings.AppSettings["url"];
                return Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + url + "/GetWeiXinUserInfo/" + "&response_type=code&scope=snsapi_userinfo&state=" + CarId.ToString() + "#wechat_redirect");


                //IAuthService authService = new AuthService();
                //WeiXinUser weiXinUser = (WeiXinUser)Session["UserInfo"];
                //User user = authService.Login(CarId,weiXinUser);

                //if (user == null)
                //{
                //    return Redirect("~/Result/ErrorInfo");
                //}
                //Session["CurrentUser"] = user;

                //return Redirect("~/Charging/ChargeThis/?GUID=" + user.GUID);
            }
            catch (Exception e)
            {
                Logger.Error("Register has an error:" + e);
                return Redirect("~/Result/Exception");
            }
            
        }

        ////
        //// GET: /Login/Details/5

        //public ActionResult Details(int id)
        //{
        //    return View();
        //}

        ////
        //// GET: /Login/Create

        //public ActionResult Create()
        //{
        //    return View();
        //}

        ////
        //// POST: /Login/Create

        //[HttpPost]
        //public ActionResult Create(FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add insert logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        ////
        //// GET: /Login/Edit/5

        //public ActionResult Edit(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /Login/Edit/5

        //[HttpPost]
        //public ActionResult Edit(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add update logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        ////
        //// GET: /Login/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /Login/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
